const util = require('../../utils/util')
Page({
  data: {
    isLoggedIn: false,
    showModal: false,
    navBarHeight: 0,
    statusBarHeight: 0,
    nowData: '' ,
    szzs:[],
    szzscolor:'',
    szcz:[],
    szczcolor:'',
    cybz:[],
    cybzcolor:'',
    userInfo: {},
    news:[]
  },

  onLoad() {
    // 获取系统信息
    const systemInfo = wx.getSystemInfoSync();
    const statusBarHeight = systemInfo.statusBarHeight; 
    const menuButtonObject = wx.getMenuButtonBoundingClientRect();
    const { top, height } = menuButtonObject;
    const navBarHeight = height + (top - statusBarHeight) * 2;
    // 尝试从本地存储中获取用户信息
    const phone = wx.getStorageSync('phone');
    const isLoggedIn = phone ? true : false;
    this.setData({
      navBarHeight:navBarHeight,
      isLoggedIn: isLoggedIn,
      statusBarHeight:statusBarHeight,
      phone: phone || {}
    });
    //获取时间
    const currentDate = new Date();
    this.setData({
      nowData: util.formatTime(currentDate)
    });
    this.getDate();
    this.getNew();
  },
  getDate(){
    //获取上证指数数据
    wx.request({
      url: 'https://api.tushare.pro',
      method:'POST',
      data:{
        token: '57572d840866f49a3e0fd74722b51e1f09273eca0c08d4824db72617',
        api_name: 'index_daily',
        params: {
          ts_code: '000001.SH',
          start_date: '20240607'
        },
        fields: ['close', 'change', 'pct_chg']
      },
      success: (res) => {
        if (res.data.code == 0) {
          const firstItem = res.data.data.items[0].map((value, index) => {
            let formattedValue = parseFloat(value).toFixed(2);
            // 对 change 和 pct_chg 添加 + 号
            if (index > 0 && value > 0) {
              formattedValue = `+${formattedValue}`;
            }
            return formattedValue;
          });
          this.setData({
            szzs:firstItem,
            szzscolor: firstItem[1] > 0 ? '#d71e06' : '#4CAF50'
          });
        } else {
          console.error('API错误:', res.data.msg);
        }
      },
      fail: (err) => {
        // 请求失败的处理
        console.error('请求失败:', err);
      }
    });
    wx.request({
      url: 'https://api.tushare.pro',
      method:'POST',
      data:{
        token: '57572d840866f49a3e0fd74722b51e1f09273eca0c08d4824db72617',
        api_name: 'index_daily',
        params: {
          ts_code: '399001.SZ',
          start_date: '20240607'
        },
        fields: ['close', 'change', 'pct_chg']
      },
      success: (res) => {
        if (res.data.code == 0) {
          const firstItem = res.data.data.items[0].map((value, index) => {
            let formattedValue = parseFloat(value).toFixed(2);
            // 对 change 和 pct_chg 添加 + 号
            if (index > 0 && value > 0) {
              formattedValue = `+${formattedValue}`;
            }
            return formattedValue;
          });
          this.setData({
            szcz:firstItem,
            szczcolor: firstItem[1] > 0 ? '#d71e06' : '#4CAF50'
          });
        } else {
          console.error('API错误:', res.data.msg);
        }
      },
      fail: (err) => {
        // 请求失败的处理
        console.error('请求失败:', err);
      }
    });
    wx.request({
      url: 'https://api.tushare.pro',
      method:'POST',
      data:{
        token: '57572d840866f49a3e0fd74722b51e1f09273eca0c08d4824db72617',
        api_name: 'index_daily',
        params: {
          ts_code: '399006.SZ',
          start_date: '20240607'
        },
        fields: ['close', 'change', 'pct_chg']
      },
      success: (res) => {
        if (res.data.code == 0) {
          const firstItem = res.data.data.items[0].map((value, index) => {
            let formattedValue = parseFloat(value).toFixed(2);
            // 对 change 和 pct_chg 添加 + 号
            if (index > 0 && value > 0) {
              formattedValue = `+${formattedValue}`;
            }
            return formattedValue;
          });
          this.setData({
            cybz:firstItem,
            cybzcolor: firstItem[1] > 0 ? '#d71e06' : '#4CAF50'
          });
        } else {
          console.error('API错误:', res.data.msg);
        }
      },
      fail: (err) => {
        // 请求失败的处理
        console.error('请求失败:', err);
      }
    })
  },
  logout() {
    wx.cloud.callFunction({
      name: 'logout',
      success: res => {
        if (res.result.success) {
          wx.showToast({
            title: '退出登录成功',
            icon: 'success',
            duration: 2000
          });
          this.setData({
            loggedIn: false
          });
          wx.setStorageSync('loggedIn', false);
        } else {
          wx.showToast({
            title: '退出登录失败',
            icon: 'error',
            duration: 2000
          });
        }
      },
      fail: err => {
        wx.showToast({
          title: '退出登录失败',
          icon: 'error',
          duration: 2000
        });
      }
    });
  },
  toSearch() {
    wx.navigateTo({
      url: '/pages/search/search'
    });
  },
  getNew(){
    wx.request({
      url: 'https://api.tushare.pro',
      method:'POST',
      data:{
        token: '57572d840866f49a3e0fd74722b51e1f09273eca0c08d4824db72617',
        api_name: 'news',
        params: {
          src:"10jqka",
          start_date:"2024-06-09 00.00.00",
          end_date: this.data.nowData
        },
      },
      success: (res) => {
        if (res.data.code == 0) {
          const news =res.data.data.items.slice(0,5);
          this.setData({
            news: news
          });
          
        } else {
          console.error('API错误:', res.data.msg);
        }
      },
      fail: (err) => {
        console.error('请求失败:', err);
      }
    })
  }
});
